#include "Kitty.h"

CKitty::CKitty() :IObject()
{
}

CKitty::~CKitty()
{
	UnloadContent();
}

void CKitty::LoadContent(LPD3DXSPRITE spriteHandler)
{
	mPos.x  = 50;
	mPos.y = GROUND_Y;

	mVel.x = 0;
	mVelLast.x = 1.0f;
	mVel.y = 0;

	mpKitty = new CSprite(spriteHandler, ENEMY_STATIC_IMAGE, 5, 1);
}

void CKitty::Input()
{
	if (CInput::Instance->GetKeyDown() == DIK_SPACE)
		this->Jump();

	if (CInput::Instance->IsKeyDown(DIK_RIGHT))
		this->GoRight();
	else
		if (CInput::Instance->IsKeyDown(DIK_LEFT))
			this->GoLeft();
		else
			this->Stop();
}

void CKitty::Update(int gameTime)
{
	mPos.x  += mVel.x * gameTime;
	mPos.y += mVel.y * gameTime;

	if (mVel.x != 0)
		mpKitty->Next();

	// Simulate fall down
	if (mPos.y < GROUND_Y) mVel.y += 0.5f;
	else
	{
		mPos.y = GROUND_Y;
		mVel.y = 0;
	}
}

void CKitty::Render()
{
	if (mVel.x > 0)
		mpKitty->Draw(mPos, SpriteEffect::None, D3DXVECTOR2(3, 3), NULL, 0, 0);
	else
		if (mVel.x < 0)
			mpKitty->Draw(mPos, SpriteEffect::FlipHorizontally, D3DXVECTOR2(3, 3), NULL, 0, 0);
		else
			if (mVelLast.x < 0)
				mpKitty->Draw(mPos, SpriteEffect::FlipHorizontally, D3DXVECTOR2(3, 3), NULL, 0, 0);
			else
				mpKitty->Draw(mPos, SpriteEffect::None, D3DXVECTOR2(3, 3), NULL, 0, 0);
}

void CKitty::UnloadContent()
{
	delete mpKitty;
}

void CKitty::GoLeft()
{
	mVel.x = -KITTY_SPEED;
	mVelLast.x = mVel.x;
}

void CKitty::GoRight()
{
	mVel.x = KITTY_SPEED;
	mVelLast.x = mVel.x;
}

void CKitty::Stop()
{
	mVel.x = 0;
	mpKitty->Reset();
}

void CKitty::Jump()
{
	if (mPos.y >= GROUND_Y) mVel.y -= 3.0f;
}
